package com.suntop.carousel.qlmediaplay.video.player;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.suntop.carousel.qlmediaplay.field.Field_State;
import com.pili.pldroid.player.PLMediaPlayer;
import com.pili.pldroid.player.PLOnBufferingUpdateListener;
import com.pili.pldroid.player.PLOnCompletionListener;
import com.pili.pldroid.player.PLOnErrorListener;
import com.pili.pldroid.player.PLOnInfoListener;
import com.pili.pldroid.player.PLOnPreparedListener;
import com.pili.pldroid.player.PLOnSeekCompleteListener;
import com.pili.pldroid.player.PLOnVideoSizeChangedListener;

import java.io.IOException;
import java.util.Map;

public class QNMediaPlayer implements IVideoPlayer, PLOnVideoSizeChangedListener
        , PLOnErrorListener, PLOnPreparedListener, PLOnBufferingUpdateListener, PLOnSeekCompleteListener
        , PLOnCompletionListener, PLOnInfoListener {
    private PLMediaPlayer mediaPlayer;
    private float lefVolme, rightVolme;
    private IVideoPlayerEventListener mListener;
    private AssetFileDescriptor fileDescriptor;
    private int current = Field_State.IDLE;
    private Map<String, String> mHeader;
    private String playURl = "";
    private String playName = "";
    private String Tag = "";
    private boolean isLoop;
    private int cacheProgress = 0;
    private boolean mIsPrepared;
    private boolean isReBack;

    public QNMediaPlayer(Context context) {
        initPlayer(context);
    }

    @Override
    public void initPlayer(Context context) {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
        mediaPlayer = new PLMediaPlayer(context);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnInfoListener(this);
        mediaPlayer.setOnVideoSizeChangedListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        mediaPlayer.setOnPreparedListener(this);
        current = Field_State.IDLE;

    }

    @Override
    public IVideoPlayer setPlayUrl(String url) {
        return setPlayUrl(url, null);

    }

    @Override
    public IVideoPlayer setPlayUrl(String url, Map<String, String> header) {
        mHeader = header;
        playName = url;
        playURl = url;
        return this;
    }

    @Override
    public IVideoPlayer setScaleType(int type) {
        return this;
    }

    @Override
    public IVideoPlayer setAssetFileDescriptor(AssetFileDescriptor fd) {
        playURl = "";
        fileDescriptor = fd;
        playName = fd.toString();
        return this;
    }

    @Override
    public IVideoPlayer setVideoTag(String tag) {
        Tag = tag;
        return this;
    }

    @Override
    public IVideoPlayer setLoop(boolean isLoop) {
        this.isLoop = isLoop;
        if (mediaPlayer != null) {
            mediaPlayer.setLooping(isLoop);
        }
        return this;
    }

    @Override
    public IVideoPlayer setVolume(float left, float right) {
        lefVolme = left;
        rightVolme = right;
        if (mediaPlayer != null) {
            mediaPlayer.setVolume(left, right);
        }
        return null;
    }

    @Override
    public IVideoPlayer setPlaySpeed(float speed) {
        if (mediaPlayer != null) {
            mediaPlayer.setPlaySpeed(speed);
        }
        return null;
    }

    @Override
    public IVideoPlayer setIVideoPlayerEventListener(IVideoPlayerEventListener eventListener) {
        mListener = eventListener;
        return this;
    }

    @Override
    public IVideoPlayer setSurface(Surface surface) {
        if (mediaPlayer != null) {
            mediaPlayer.setSurface(surface);
        }
        return this;
    }

    @Override
    public IVideoPlayer setDisplay(SurfaceHolder holder) {
        if (mediaPlayer != null) {
            mediaPlayer.setDisplay(holder);
        }
        return this;
    }

    @Override
    public IVideoPlayer seekTo(long time) {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(time);
        }
        return this;
    }

    @Override
    public boolean isOnPlaying() {
        return mediaPlayer != null && (current == Field_State.PAUSED
                || current == Field_State.PREPARED
                || current == Field_State.PLAYING
                || current == Field_State.COMPLETED
                || mediaPlayer.isPlaying());
    }

    @Override
    public boolean isNeedRest() {
        boolean isNeed = current == Field_State.ERROR
                || current == Field_State.IDLE || current == Field_State.INTERRUPT
                || current == Field_State.DESTROYED;
        return isNeed;
    }

    @Override
    public boolean isReBack() {
        return isReBack;
    }

    @Override
    public boolean mIsPrepared() {
        return mIsPrepared;
    }

    @Override
    public String takePlayUrl() {
        return playName;
    }

    @Override
    public String takePlayTag() {
        return Tag;
    }

    @Override
    public int getPlayState() {
        return current;
    }

    @Override
    public long getCurrentPosition() {
        return mediaPlayer != null ? mediaPlayer.getCurrentPosition() : 0;
    }

    @Override
    public long getDuration() {
        return mediaPlayer != null ? mediaPlayer.getDuration() : 0;
    }

    @Override
    public int getBufferedPercentage() {
        return cacheProgress;
    }

    @Override
    public float getSpeed() {
        return 0;
    }

    @Override
    public long getTcpSpeed() {
        return mediaPlayer != null ? mediaPlayer.getVideoFps() : 0;
    }

    @Override
    public int getVideoWidth() {
        return mediaPlayer != null ? mediaPlayer.getVideoWidth() : 0;
    }

    @Override
    public int getVideoHeight() {
        return mediaPlayer != null ? mediaPlayer.getVideoHeight() : 0;
    }

    @Override
    public void setIsPrepared(boolean isPrepared) {
        mIsPrepared = isPrepared;

    }

    @Override
    public void setIsReBack(boolean isReBack) {
        this.isReBack=isReBack;
    }

    @Override
    public void prepareAsync() {
        if (mediaPlayer == null) {
            return;
        }
        try {
            cacheProgress = 0;
            if (fileDescriptor != null) {
                Log.e("播放状态", "不支持的播放内容");
            } else if (!TextUtils.isEmpty(playURl)) {
                if (mHeader != null)
                    mediaPlayer.setDataSource(playURl, mHeader);
                else
                    mediaPlayer.setDataSource(playURl);
            }
            current = Field_State.PREPARING;
            mediaPlayer.prepareAsync();
        } catch (IOException exception) {
            current = Field_State.ERROR;
            mediaPlayer.stop();
            Log.e("播放器错误", exception + "");
            if (mListener != null) {
                mListener.onError(Tag, exception + "");
            }
            exception.printStackTrace();
        }
    }

    @Override
    public void start() {
        if (mediaPlayer!=null){
            if (isOnPlaying()){
                current = Field_State.PLAYING;
                mediaPlayer.start();
            }
        }

    }

    @Override
    public void pause() {
        if (mediaPlayer!=null){
            if (isOnPlaying()){
                current = Field_State.PAUSED;
                mediaPlayer.pause();
            }
        }
    }

    @Override
    public void stop() {
        if (mediaPlayer!=null){
            if (isOnPlaying()){
                current = Field_State.INTERRUPT;
                mediaPlayer.stop();
            }
        }
    }

    @Override
    public void reset() {
        if (mediaPlayer!=null){
            if (isOnPlaying()){
                mediaPlayer.stop();
            }
            mediaPlayer.setSurface(null);
            mediaPlayer.setDisplay(null);
        }
        current = Field_State.IDLE;
    }

    @Override
    public void release() {
        if (mediaPlayer!=null){
            if (isOnPlaying()){
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer=null;
        }
        current = Field_State.DESTROYED;
    }

    @Override
    public void onVideoSizeChanged(int i, int i1) {
        int videoWidth = mediaPlayer.getVideoWidth();
        int videoHeight = mediaPlayer.getVideoHeight();
        if (videoWidth != 0 && videoHeight != 0 && mListener != null) {
            mListener.onVideoSizeChanged(Tag, videoWidth, videoHeight);
        }
    }

    @Override
    public void onBufferingUpdate(int i) {
        cacheProgress=i;
    }



    @Override
    public void onPrepared(int i) {
        current = Field_State.PREPARED;
        if (mListener != null) {
            mListener.onPrepared(Tag);
        }
        if (mIsPrepared) {
            mediaPlayer.start();
        }
    }

    @Override
    public void onSeekComplete() {
         if (mIsPrepared){
             
         }
    }

    @Override
    public void onCompletion() {
        current = Field_State.COMPLETED;
//        seekTo(0);
        stop();
        if (mListener != null) {
            mListener.onCompletion(Tag);
        }
    }


    @Override
    public boolean onError(int what, Object o) {
        current = Field_State.ERROR;
        if (mListener != null) {
            mListener.onError(Tag, "错误 what=" + what + " extra=" );
        }
        reset();
        return false;
    }

    @Override
    public void onInfo(int what, int extra ,Object o) {
        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
            current = Field_State.PLAYING;
        }
        if (mListener != null) {
            mListener.onInfo(Tag, what, extra);
        }
    }
}
